function getNowFormatDate(theDate){
    var day = theDate;
    var Year = 0;
    var Month = 0;
    var Day = 0;
    var CurrentDate = "";
    // 初始化时间
    Year= day.getFullYear();// ie火狐下都可以
    Month= day.getMonth()+1;
    Day = day.getDate();
    CurrentDate += Year + "-";
    if (Month >= 10) {
        CurrentDate += Month + "-";
    } else {
        CurrentDate += "0" + Month + "-";
    }
    if (Day >= 10 ){
        CurrentDate += Day ;
    } else {
        CurrentDate += "0" + Day ;
    }
    return CurrentDate;
}

//这个方法将取得某年(year)第几周(weeks)的星期几(weekDay)的日期
function getXDate(year,weeks,weekDay){
    // 用指定的年构造一个日期对象，并将日期设置成这个年的1月1日
    // 因为计算机中的月份是从0开始的,所以有如下的构造方法
    var date = new Date(year,"0","1");
    // 取得这个日期对象 date 的长整形时间 time
    var time = date.getTime();
    // 将这个长整形时间加上第N周的时间偏移
    // 因为第一周就是当前周,所以有:weeks-1,以此类推
    // 7*24*3600000 是一星期的时间毫秒数,(JS中的日期精确到毫秒)
    time+=(weeks-1)*7*24*3600000;
    // 为日期对象 date 重新设置成时间 time
    date.setTime(time);
    return getNextDate(date,weekDay);
}
// 这个方法将取得 某日期(nowDate) 所在周的星期几(weekDay)的日期
function getNextDate(nowDate,weekDay){
    // 0是星期日,1是星期一,...
    weekDay%=7;
    var day = nowDate.getDay();
    var time = nowDate.getTime();
    var sub = weekDay-day;
    if(sub <= 0){
        sub += 7;
    }
    time+=sub*24*3600000;
    nowDate.setTime(time);
    return nowDate;
}

function isInOneYear(_year,_week){
    if(_year == null || _year == '' || _week == null || _week == ''){
        return true;
    }
    var theYear = getXDate(_year,_week,7).getFullYear();
    if(theYear != _year){
        return false;
    }
    return true;
}

// 获取日期范围显示
function getDateRange(_year,_week){
    var beginDate;
    var endDate;
    if(_year == null || _year == '' || _week == null || _week == ''){
        return "";
    }
    _week = _week - 1;
    beginDate = getXDate(_year,_week,1);
    endDate = getXDate(_year,(_week - 0 + 1),7);
    return getNowFormatDate(beginDate) + " 至 "+ getNowFormatDate(endDate);
}

//日期处理
function dateRange(obj){
    //获取当前年
    var year = $("#yearId").val();
    // var myDate = new Date();
    // var year = myDate.getFullYear();
    // layer.msg("提示：当前是"+year+"第"+theWeek()+"周");
    if(isInOneYear(year,obj.value)){
        var showDate = getDateRange(year,obj.value);
        document.getElementById("_dateRange_import").value=showDate;
    }else{
        layer.msg(year+"年无"+obj.value+"周，请重新选择");
        return;
    }
}

/**
* 获取当前年份共有多少周次(本次未用到)
*/
function getNumOfWeeks(){
    var myDate = new Date();
    var year = myDate.getFullYear();  //获取当前年
    var d=new Date(year,0,1);
    var yt=(( year%4==0 && year%100!=0) || year%400==0)? 366:365;
    var allwk = Math.ceil((yt-d.getDay())/7.0);
    return allwk;
}

//获取当前日期在当前年第几周函数封装
function theWeek() {
    var totalDays = 0;
    now = new Date();
    years = now.getYear()
    if (years < 1000)
        years += 1900
    var days = new Array(12);
    days[0] = 31;
    days[2] = 31;
    days[3] = 30;
    days[4] = 31;
    days[5] = 30;
    days[6] = 31;
    days[7] = 31;
    days[8] = 30;
    days[9] = 31;
    days[10] = 30;
    days[11] = 31;

    //判断是否为闰年，针对2月的天数进行计算
    if (Math.round(now.getYear() / 4) == now.getYear() / 4) {
        days[1] = 29
    } else {
        days[1] = 28
    }
    if (now.getMonth() == 0) {
        totalDays = totalDays + now.getDate();
    } else {
        var curMonth = now.getMonth();
        for (var count = 1; count <= curMonth; count++) {
            totalDays = totalDays + days[count - 1];
        }
        totalDays = totalDays + now.getDate();
    }
    //得到第几周
    var week = Math.ceil(totalDays / 7);//原代码使用的是round
    return week;
}

//根据选择年份获取周次
function getWeeks(obj){
    //年份变更，更改周次对应日期
    $("#_dateRange_import").val("");
    var year = obj.value;
    var d=new Date(year,0,1);
    var yt=(( year%4==0 && year%100!=0) || year%400==0)? 366:365;
    var allwk = Math.ceil((yt-d.getDay())/7.0);
    //获取当前年份，判断选择年份是否是当前年份
    var myDate = new Date();
    var curYear = myDate.getFullYear();
    var curweek = document.getElementById("dateSearch");

    if(curYear==year){//如果是当前年份，获取当前日子的周次
        // var theweek = theWeek();
        var str = "";
        str+="周次：<select id=\"dateSearch\" onchange=\"dateRange(this)\"><option value=\"\">请选择周次</option>";
        for(var i=1;i<=allwk;i++){
            str+="<option value="+i+">第"+i+"周</option>" ;
        }
        str+= "</select>";
        curweek.innerHTML = str ;
    }else{//如果不是获取年份内的所有周次值
        var str = "";
        str+="周次：<select id=\"dateSearch\"onchange=\"dateRange(this)\"><option value=\"\">请选择周次</option>";
        for(var i=1;i<=allwk;i++){
            str+="<option value="+i+">第"+i+"周</option>" ;
        }
        str+= "</select>";
        curweek.innerHTML = str ;
    }

}

